<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>判断数字并求和</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
    }

    label {
      display: block;
      margin-bottom: 5px;
    }

    input {
      margin-bottom: 10px;
      padding: 8px;
      width: 150px;
    }

    button {
      padding: 8px 15px;
      background-color: #007BFF;
      color: white;
      border: none;
      cursor: pointer;
    }

    button:hover {
      background-color: #0056b3;
    }

    #result {
      margin-top: 15px;
      font-weight: bold;
    }
  </style>
</head>

<body>
  <label for="inputNumber">请输入一个数:</label>
  <input type="text" id="inputNumber">
  <button onclick="checkAndSum()">判断并求和</button>
  <div id="result"></div>

  <script>
    function checkAndSum() {
      const inputValue = document.getElementById('inputNumber').value;
      const num = parseFloat(inputValue);

      if (isNaN(num)) {
        document.getElementById('result').innerHTML = "请输入有效的数字";
      } else {
        let sum = 0;
        for (let i = 1; i <= num; i++) {
          sum += i;
        }
        document.getElementById('result').innerHTML = `从 1 加到 ${num} 的和是: ${sum}`;
      }
    }
  </script>
</body>

</html>